package com.ruoyi.project.cms.notice.service;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.notice.mapper.CmsNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.ruoyi.project.cms.notice.mapper.NoticeTypeMapper;
import com.ruoyi.project.cms.notice.domain.NoticeType;

import java.util.List;

/**
 *com.ruoyi.project.cms.notice.service
 *作者：lh
 描述:
 *时间:2020-05-17
 */
@Service("type")
public class NoticeTypeServiceImpl implements NoticeTypeService {

    @Resource
    private NoticeTypeMapper cmsNoticeTypeMapper;

    @Autowired
    private CmsNoticeMapper cmsNoticeMapper;

    @Override
    public int deleteByPrimaryKey(int tid) {
        return cmsNoticeTypeMapper.deleteByPrimaryKey(tid);
    }

    @Override
    public int insert(NoticeType record) {
        return cmsNoticeTypeMapper.insert(record);
    }

    @Override
    public int insertSelective(NoticeType record) {
        return cmsNoticeTypeMapper.insertSelective(record);
    }

    @Override
    public NoticeType selectByPrimaryKey(int tid) {
        return cmsNoticeTypeMapper.selectByPrimaryKey(tid);
    }

    @Override
    public int updateByPrimaryKeySelective(NoticeType record) {
        return cmsNoticeTypeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(NoticeType record) {
        return cmsNoticeTypeMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<NoticeType> selectNoticeTypeList(NoticeType cmsNoticeType) {
        return cmsNoticeTypeMapper.selectNoticeTypeList(cmsNoticeType);
    }

    @Override
    public String checkNoticeTypeNameUnique(NoticeType cmsNoticeType) {

        int tid = StringUtils.isNull(cmsNoticeType.getTid()) ? -1: cmsNoticeType.getTid();
        NoticeType info = cmsNoticeTypeMapper.checkNoticeTypeNameUnique(cmsNoticeType.getTname());
        if (StringUtils.isNotNull(info))
        {
            return "1";
        }
        return "0";
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteNoticeByIds(String ids) {
        Integer[] tids = Convert.toIntArray(ids);
        for (int tid : tids)
        {
            NoticeType ctp = selectByPrimaryKey(tid);
            if (cmsNoticeMapper.selectNotice(tid)!=null){
                throw new BusinessException(String.format("%1$s有公告,不能删除", ctp.getTname()));
            }
            cmsNoticeTypeMapper.deleteByPrimaryKey(tid);
        }
        return 1;
    }

    @Override
    public NoticeType selectByName(String tname) {
        return cmsNoticeTypeMapper.selectByName(tname);
    }

    @Override
    public List<NoticeType> selectAll() {
        return cmsNoticeTypeMapper.selectAll();
    }






}
